package org.shirdrn.document.preprocessing.driver;

import java.io.IOException;
import java.util.Properties;

import org.shirdrn.document.preprocessing.api.Context;
import org.shirdrn.document.preprocessing.api.ProcessorType;
import org.shirdrn.document.preprocessing.core.component.BasicInformationCollector;
import org.shirdrn.document.preprocessing.core.component.DocumentTFIDFComputation;
import org.shirdrn.document.preprocessing.core.component.DocumentWordsCollector;
import org.shirdrn.document.preprocessing.core.component.test.LoadFeatureTermVector;
import org.shirdrn.document.preprocessing.core.component.test.OutputtingQuantizedTestData;
import org.shirdrn.document.preprocessing.driver.common.AbstractDocumentProcessorDriver;
import org.springframework.web.socket.WebSocketSession;
import org.shirdrn.document.preprocessing.core.utils.PreprocessingUtils;

/**
 * The driver for starting components to process TEST data set.
 * It includes the following 5 components:
 * <ol>
 * <li>{@link BasicInformationCollector}</li>
 * <li>{@link DocumentWordsCollector}</li>
 * <li>{@link LoadFeatureTermVector}</li>
 * <li>{@link DocumentTFIDFComputation}</li>
 * <li>{@link OutputtingQuantizedTestData}</li>
 * </ol>
 * Executing above components in order can output the normalized
 * data for feeding libSVM classifier developed by <code>Lin Chih-Jen</code>
 * (<a href="www.csie.ntu.edu.tw/~cjlin/libsvm/‎">www.csie.ntu.edu.tw/~cjlin/libsvm/‎</a>)
 * 
 * @author Shirdrn
 */
public class TestDocumentPreprocessingDriver extends AbstractDocumentProcessorDriver {

	@Override
	public void preprocess(String uid,WebSocketSession session,Properties p) {
		Context context = super.newContext(ProcessorType.TEST, p);
		context.setVectorMetadata(PreprocessingUtils.newVectorMetadata());
	
		context.setUserId(uid);
		context.setWebSocketSession(session);
		// build component chain for test data
		Class<?>[] classes = new Class[] {
				BasicInformationCollector.class,
				DocumentWordsCollector.class,
				LoadFeatureTermVector.class,
				DocumentTFIDFComputation.class,
				OutputtingQuantizedTestData.class
		};
		run(PreprocessingUtils.newChainedComponents(context, classes));
		
		try {
			if(context.getWebSocketSession()!=null ){
				context.getWebSocketSession().close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//处理完毕之后关闭websocket连接.
		}
	}
	public static void startProc(String uid,WebSocketSession s,Properties p) {
		AbstractDocumentProcessorDriver.start(TestDocumentPreprocessingDriver.class,uid,s,p);		
	}

}
